package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.enums.CacheKey;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.CollectionUtils;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.redis.cache.CacheTemplate;
import com.ruoyi.dynamic.mapper.RoomUserPostMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.pojo.entity.Room;
import com.ruoyi.dynamic.model.pojo.entity.RoomPost;
import com.ruoyi.dynamic.model.pojo.entity.RoomUserPost;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomBanByUserRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomUserPostSearchRequest;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomMaiUserResult;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomUserPostInfoResult;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomUserPostResult;
import com.ruoyi.dynamic.service.IRoomPostService;
import com.ruoyi.dynamic.service.IRoomService;
import com.ruoyi.dynamic.service.IRoomUserPostPermissionService;
import com.ruoyi.dynamic.service.IRoomUserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 房间马甲设置 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2025-01-14
 */
@Service
public class RoomUserPostServiceImpl extends ServiceImpl<RoomUserPostMapper, RoomUserPost> implements IRoomUserPostService {

    @Autowired
    private IRoomUserPostPermissionService iRoomUserPostPermissionService;
    @Autowired
    private IRoomPostService iRoomPostService;
    @Autowired
    private IRoomService iRoomService;

    @Override
    public RoomUserPost verify(Long id) {
        RoomUserPost userPost = getById(id);
        Assert.notNull(userPost, "此管理员用户不存在");
        return userPost;
    }

    @Override
    public Pages<AppRoomUserPostInfoResult> listPage(AppRoomUserPostSearchRequest request, Long userId) {

        Page<RoomUserPost> roomUserPostPage = PageUtils.initPage(request, RoomUserPost.class);

        Page<RoomUserPost> page = lambdaQuery()
                .eq(RoomUserPost::getRoomId, request.getRoomId())
                .orderByAsc(RoomUserPost::getCreateTime)
                .page(roomUserPostPage);

        List<AppRoomUserPostInfoResult> results = CollectionUtils.emptyList();
        for (RoomUserPost record : page.getRecords()) {
            results.add(getInfo(record));
        }
        return PageUtils.pages(page, results);
    }

    @Override
    public AppRoomUserPostInfoResult info(Long id) {
        RoomUserPost verify = verify(id);
        return getInfo(verify);
    }

    @Override
    public AppRoomUserPostResult lookUser(AppRoomBanByUserRequest request, Long userId) {
        String key = CacheKey.ROOM_MAI.getKey(String.valueOf(userId));
        String cacheKey = key.replaceAll("\\{roomNumber}", String.valueOf(request.getRoomId()));
        AppRoomMaiUserResult result = CacheTemplate.getCache(cacheKey, AppRoomMaiUserResult.class);


        String lianKey = CacheKey.ROOM_LIAN.getKey(String.valueOf(userId));
        String lianKeycacheKey = lianKey.replaceAll("\\{roomNumber}", String.valueOf(request.getRoomId()));
        AppRoomMaiUserResult linResult = CacheTemplate.getCache(lianKeycacheKey, AppRoomMaiUserResult.class);


        return null;
    }

    @Override
    public List<RoomPost> listByUser(Long roomId, Long userId) {
        Room verify = iRoomService.verify(roomId);
        if (verify.getUserId().equals(userId)) {
            return iRoomPostService.list();
        }
        RoomUserPost one = lambdaQuery()
                .eq(RoomUserPost::getRoomId, roomId)
                .eq(RoomUserPost::getUserId, userId)
                .one();
        if (CommonUtils.isNull(one)) return CollectionUtils.emptyList();
        RoomPost post = iRoomPostService.getById(one.getPostId());
        return iRoomPostService.lambdaQuery().lt(RoomPost::getSort, post.getSort()).list();
    }


    AppRoomUserPostInfoResult getInfo(RoomUserPost post) {
        return AppRoomUserPostInfoResult.builder()
                .id(post.getId())
                .postIconUrl(post.getPostIconUrl())
                .postId(post.getPostId())
                .postName(post.getPostName())
                .roomId(post.getRoomId())
                .userId(post.getUserId())
                .userNickName(post.getUserNickName())
                .userPhoto(post.getUserPhoto())
                .userPostPermissions(iRoomUserPostPermissionService.getPerByUser(post.getId()))
                .build();
    }


}
